package net.gazhi.delonix.rbac.entity;

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;

/**
 * 当前登录用户
 * 
 * @author Jeffrey Lin
 *
 */
public class LoginUser {

	/**
	 * 超级管理员的ID=1
	 */
	public static final int SUPER_ADMIN_ID = 1;

	public static final int GUEST_ID = -1;

	public static final String LOGIN_USER_ATTRIBUTE = "loginUser";

	public static final LoginUser guest() {
		return new LoginUser(null, GUEST_ID, "guest", "guest", new LinkedHashSet<MenuItem>());
	}

	/**
	 * user id
	 */
	private int id;

	private String loginName;

	private String name;

	private String ticketNo;

	private int currentActionId;

	private String currentActionPath;

	private Map<String, Boolean> grantedPaths = new HashMap<String, Boolean>();

	private LinkedHashSet<MenuItem> rootMemuItems;

	public LoginUser(String ticketNo, int id, String loginName, String name, LinkedHashSet<MenuItem> rootMemuItems) {
		this.ticketNo = ticketNo;
		this.id = id;
		this.loginName = loginName;
		this.name = name;
		this.rootMemuItems = rootMemuItems;
	}

	/**
	 * 判断当前用户是否有访问某个 action
	 * 
	 * @param actionId
	 * @return
	 */
	public boolean hasAuth(String path) {
		return this.getGrantedPaths().containsKey(path);
	}

	/**
	 * 是否已未登录访客
	 * 
	 * @return
	 */
	public boolean isGuest() {
		return this.getIsGuest();
	}

	/**
	 * 是否已未登录访客
	 * 
	 * @return
	 */
	public boolean getIsGuest() {
		return this.id == GUEST_ID;
	}

	/**
	 * 是否超级管理员
	 * 
	 * @return
	 */
	public boolean isSuperAdmin() {
		return this.getIsSuperAdmin();
	}

	/**
	 * 是否超级管理员
	 * 
	 * @return
	 */
	public boolean getIsSuperAdmin() {
		return this.id == SUPER_ADMIN_ID;
	}

	public int getId() {
		return id;
	}

	public String getLoginName() {
		return loginName;
	}

	public String getName() {
		return name;
	}

	public String getTicketNo() {
		return this.ticketNo;
	}

	public int getCurrentActionId() {
		return currentActionId;
	}

	public void setCurrentActionId(int currentActionId) {
		this.currentActionId = currentActionId;
	}

	public String getCurrentActionPath() {
		return currentActionPath;
	}

	public void setCurrentActionPath(String currentActionPath) {
		this.currentActionPath = currentActionPath;
	}

	public Map<String, Boolean> getGrantedPaths() {
		return grantedPaths;
	}

	public LinkedHashSet<MenuItem> getRootMemuItems() {
		return rootMemuItems;
	}

}
